package com.ctcemti.mdm.project.sys.service.impl;

import com.ctcemti.mdm.common.exception.BusinessException;
import com.ctcemti.mdm.common.util.BatchOperationUtils;
import com.ctcemti.mdm.common.util.JWTUtil;
import com.ctcemti.mdm.project.sys.dao.RelJobMenuMapper;
import com.ctcemti.mdm.project.sys.dao.SysMenuMapper;
import com.ctcemti.mdm.project.sys.entity.RelJobMenu;
import com.ctcemti.mdm.project.sys.entity.SysMenu;
import com.ctcemti.mdm.project.sys.service.ISysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.util.StringUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
public class SysMenuServiceImpl implements ISysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;
    @Autowired
    private RelJobMenuMapper relJobMenuMapper;


    @Transactional
    @Override
    public int deleteByPrimaryKey(Integer id) {
        return sysMenuMapper.deleteByPrimaryKey(id);
    }

    @Transactional
    @Override
    public int insert(SysMenu record) {
        return sysMenuMapper.insert(record);
    }

    @Transactional
    @Override
    public int insertSelective(SysMenu record) {
        return sysMenuMapper.insertSelective(record);
    }

    @Override
    public SysMenu selectByPrimaryKey(Integer id) {
        return sysMenuMapper.selectByPrimaryKey(id);
    }

    @Transactional
    @Override
    public int updateByIdSelective(SysMenu record) {
        return sysMenuMapper.updateByIdSelective(record);
    }

    @Transactional
    @Override
    public int updateByPrimaryKey(SysMenu record) {
        return sysMenuMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<SysMenu> selectAllMenu() {
        return sysMenuMapper.selectAllForSort();
    }

    @Override
    public List<SysMenu> selectByPid(Map<String,Object> params) {
        return sysMenuMapper.selectByPid(params);
    }

    @Override
    public List<SysMenu> selectMenu(SysMenu sysMenu) {
        return sysMenuMapper.select(sysMenu);
    }

    @Override
    public List<SysMenu> selectMenuByMenuName(Map<String,Object> params){
        return sysMenuMapper.selectMenuByMenuName(params);
    }

    @Override
    public int deleteByPrimaryKeys(String ids) {
        List<String> keys = sysMenuMapper.selectMenuByIds(ids.split(","));
        String.join(",", keys);
        return sysMenuMapper.deleteByIds(String.join(",", keys));
    }

    @Override
    public List<SysMenu> selectById(Map<String,Object> map) {
        return sysMenuMapper.selectByIdForIndex(map);
    }

    @Override
    public int bindJobAndMenu(String jobid, String menuids) {
        List<RelJobMenu> list = new ArrayList<RelJobMenu>();
        if(StringUtil.isEmpty(jobid)){
            throw new BusinessException("非法删除，岗位id不可为空！");
        }
        if(!StringUtil.isEmpty(menuids)){
            String[] menuid = menuids.split(",");
            String[] jobids = jobid.split(",");
            relJobMenuMapper.deleteByJobIds(Arrays.asList(jobid.split(",")));
            for(int j=0;j<jobids.length;j++){
                for(int i=0;i<menuid.length;i++){
                    RelJobMenu relJobMenu = new RelJobMenu();
                    relJobMenu.setJobId(jobids[j]);
                    relJobMenu.setMenuId(menuid[i]);
                    list.add(relJobMenu);
                }
            }
            BatchOperationUtils<RelJobMenu> batchOperationUtils2 = new BatchOperationUtils();
            //添加新的菜单项
            batchOperationUtils2.batchOperation(list, 400,var->relJobMenuMapper.insertBatch(var));
            return 1;
        }else{
            //删除当前jobid对应的所有menu
            return relJobMenuMapper.deleteByJobIds(Arrays.asList(jobid.split(",")));
        }
    }

    /* *
     * @Author bxf
     * @Description 根据岗位id获取菜单（去重）
     * @Date 19:19 2019/8/24
     * @param list
     * @return java.util.List<com.ctcemti.mdm.project.sys.entity.SysMenu>
     */
    public List<SysMenu> selectAllMenuByJob(List<String> list){
        return sysMenuMapper.selectAllMenuByJob(list);
    }

    @Override
    public List<SysMenu> selectMenusFromRole(HttpServletRequest request) {
        List<SysMenu> list = new ArrayList<SysMenu>();
        String token = request.getHeader("Authorization");
        String loginId = JWTUtil.getLoginId(token);
        String loginName = JWTUtil.getLoginName(token);
        if("Administrator".equals(loginName)){
            list = sysMenuMapper.selectAllForSort();
        }else{
            list = sysMenuMapper.selectMenusFromRole(loginId);
        }
        return list;
    }

    @Override
    public List<String> selectPermissionsFromRole(HttpServletRequest request) {
        List<String> list = new ArrayList<String>();
        String token = request.getHeader("Authorization");
        String loginId = JWTUtil.getLoginId(token);
        String loginName = JWTUtil.getLoginName(token);
        list = sysMenuMapper.selectPermissionsFromRole(loginId);
        return list;
    }

}
